<?php
	//权限分类类，权限必须属于某一个分类
	class privilege_type
	{
		var $database = null;

		function privilege_type()
		{
			$this->database = load( 'database' );
		}

		//返回权限分类列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : 'id asc';
			$count = 'from privilege_type';
			$list = 'select * ' . $count . ' order by ' . $order;
			$result = $this->database->split( $count, $list, $size, $page );
			return $result;
		}

		//获取某权限分类数据
		function get( $data )
		{
			$result = array();
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) ) $result = $this->database->unique( 'select * from privilege_type where id = ' . $data['id'] );
			return $result;
		}

		//修改某权限分类
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['id']['kind'] = '';
			$field['id']['type'] = 'num';
			$field['id']['min'] = '1';
			$field['id']['max'] = '8';
			$field['id']['info'] = '请传递ID';

			$field['name']['kind'] = '';
			$field['name']['type'] = 'text';
			$field['name']['min'] = '1';
			$field['name']['max'] = '32';
			$field['name']['info'] = '请输入名称';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$this->database->set( 'privilege_type', $data, array( 'id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//创建某权限分类
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['name']['kind'] = '';
			$field['name']['type'] = 'text';
			$field['name']['min'] = '1';
			$field['name']['max'] = '32';
			$field['name']['info'] = '请输入名称';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$id = $this->database->add( 'privilege_type', $data );

				if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $id;
				}
				else
				{
					$result['message'][] = '执行创建失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}
	}
?>